<template>
  <div class="swiper">
    <swiper
      :slidesPerView="1"
      :autoplay="{ delay: 30000, disableOnInteraction: false }"
      :navigation="true"
      :pagination="{ type: 'bullets', clickable: true }"
      :scrollbar="{ draggable: false }"
      :space-between="30"
      :modules="modules"
      :loop="true"
      @mouseenter="enter"
      @mouseleave="leave"
      @swiper="onSwiper"
      @slideChange="onSlideChange"
    >
      <swiper-slide
        @click="goProductFn(item)"
        v-for="(item, index) in swiperData"
        :key="index"
        class="swiperItem"
      >
        <div class="Sample">
          <div class="SampleBoxImg">
            <img :src="$t(`messages.home.filmList[${index}].bakImg`)" alt="" srcset="" />
          </div>
          <div class="SampleBox aliCenter">
            <div class="SampleBoxLeft">
              <div class="SampleBoxLeftTop">
                <div class="title">{{ $t(`messages.home.filmList[${index}].leftTitle`) }}</div>
                <div class="size">
                  {{ $t(`messages.home.filmList[${index}].leftDetils`) }}
                </div>
              </div>
              <div class="SampleBoxLeftButtom">
                <div class="title2">{{ $t(`messages.home.fimTitle`) }}</div>
                <div class="imageBox aliCenter">
                  <img :src="$t(`messages.home.filmList[${index}].image1`)" alt="" srcset="" />
                  <img :src="$t(`messages.home.filmList[${index}].image2`)" alt="" srcset="" />
                </div>
              </div>
            </div>
            <div class="SampleBoxRigth">
              <ul class="ul">
                <li class="li" v-for="(item2, index2) in item.rigthTitle" :key="index2">
                  <div class="title2">
                    {{ $t(`messages.home.filmList[${index}].rigthTitle[${index2}]`) }}
                  </div>
                  <div class="size">
                    {{ $t(`messages.home.filmList[${index}].rigthDetils[${index2}]`) }}
                  </div>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { homeFilm } from '@/http/api/home.js'
import Zh from '@/assets/i18n/langs/zh.js'
import En from '@/assets/i18n/langs/en.js'
import { toRaw, ref, reactive, onMounted } from 'vue'
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/css'
import 'swiper/scss/navigation'
import 'swiper/scss/pagination'
import { Autoplay, Navigation, Scrollbar, A11y } from 'swiper/modules'
const props = defineProps()
const router = useRouter()
let modules = [Autoplay, Navigation, Scrollbar, A11y]
//定义swiperNew,目的获取非响应式swiper
let swiperNew = ''

//鼠标移入
const enter = () => {
  swiperNew.autoplay.stop()
}
//鼠标移出
const leave = () => {
  swiperNew.autoplay.start()
}
const onSwiper = (swiper) => {
  // console.log(swiper);

  swiperNew = toRaw(swiper) //拿到swiper对象再转换为非响应式
}
const onSlideChange = () => {
  // console.log("slide change");
}
let pageData = reactive({
  page: 1,
  size: 50
})
let swiperData = ref([])
const getData = async () => {
  let res = await homeFilm(pageData)
  let allTilte = res.res.map((item) => {
    return {
      rigthTitleZh: item.rigthZh.split('/'),
      rigthTitleEn: item.rigthEn.split('/'),
      rigthDetilsZh: item.rigthDetilsZh.split('/'),
      rigthDetilsEn: item.rigthDetilsEn.split('/'),
    }
  })
  Zh.messages.home.filmList = res.res.map((item, index) => {
    return {
      leftTitle: item.leftTitleZh,
      leftDetils: item.leftDetilsZh,
      image1: item.image1Zh,
      image2: item.image2Zh,
      bakImg: item.bakImgZh,
      rigthTitle: allTilte[index].rigthTitleZh,
      rigthDetils: allTilte[index].rigthDetilsZh,
    }
  })
  En.messages.home.filmList = res.res.map((item, index) => {
    return {
      leftTitle: item.leftTitleEn,
      leftDetils: item.leftDetilsEn,
      image1: item.image1En,
      image2: item.image2En,
      bakImg: item.bakImgEn,
      rigthTitle: allTilte[index].rigthTitleEn,
      rigthDetils: allTilte[index].rigthDetilsEn,
    }
  })

  swiperData.value = res.res.map((item, index) => {
    return {
      leftTitle: item.leftTitleEn,
      leftDetils: item.leftDetilsEn,
      image1: item.image1En,
      image2: item.image2En,
      bakImg: item.bakImgEn,
      rigthTitle: allTilte[index].rigthTitleEn,
      rigthDetils: allTilte[index].rigthDetilsEn,
      type: item.type
    }
  })
}
// 点击轮播图跳转页面
const goProductFn = (data) => {
  let filmNam = [{
    url: 'paint-protection-film-PPF',
    type: 'Paint Protection Film (PPF)'
  }, {
    url: 'color-paint-protection-film-PPF',
    type: 'Color Change Film'
  }, {
    url: 'window-film',
    type: 'Window Tint Film'
  }, {
    url: 'sunroof-film',
    type: 'Roof Wrap Film'
  }]
  filmNam.forEach((item) => {
    if (data.type.includes(item.type)) {
      router.push(`/product/${item.url}`)
    }
  })

}
onMounted(() => {
  getData()
})
</script>
<style lang="scss" scoped>
.swiper {
  width: 100%;
  height: 100%;
  margin: 0;
  img {
    width: 100%;
    height: 100%;
  }

  .swiperItem {
    height: 100%;
    width: 100% !important;
    cursor: pointer;
  }
}

::v-deep(.swiper-pagination-bullet) {
  //修改分页器圆点大小
  width: 8px;
  height: 8px;
  background-color: #fff;
}

.swiper-horizontal > .swiper-pagination-bullets .swiper-pagination-bullet,
::v-deep(.swiper-pagination-horizontal.swiper-pagination-bullets) .swiper-pagination-bullet {
  // 分页器远点之间的距离
  margin: 0 10px;
}

::v-deep(.swiper-button-prev),
::v-deep(.swiper-button-next) {
  color: black;
  width: 70px;
  height: 70px;
  background: #ffffff;
  border-radius: 35px;
}
.imgTitle {
  .title {
    font-size: 32px;
    font-weight: bold;
    color: #ffdd00;
  }
  .size {
    width: 330px;
    color: #ffffff;
    white-space: pre-wrap; /* 保留空白符序列，但是当到达容器边界时进行换行 */
    overflow-wrap: break-word; /* 在长单词或URL地址内部进行换行 */
    word-break: break-all; /* 允许在任意字符间断开（用于CJK文字等） */
  }
}
.SampleBoxLeft {
  display: grid;
  align-content: space-between;
}
.SampleBoxImg {
  width: 100%;
  height: 550px;
  position: relative;
  left: 0;
  top: 0;
  z-index: -9;
  img {
    width: 100%;
    height: 100%;
  }
}
.Sample {
  height: 1500px;
  width: 100%;
  position: relative;
  // background-image: url('../../../assets/image/swiper/2.png');
  .SampleBox {
    height: 600px;
    width: 100%;
    position: absolute;
    left: 0;
    bottom: 170px;
    justify-content: space-around;
  }
  .SampleBoxLeftButtom {
    margin-top: 80px;
    .imageBox {
      margin-top: 30px;
      justify-content: space-between;

      img {
        border-radius: 20px;
        overflow: hidden;
        width: 300px;
        height: 250px;
      }
    }
  }
  .SampleBoxRigth {
    .ul {
      display: grid;
      align-content: space-between;
      height: 100%;
    }
    .li {
      margin-top: 35px;
    }
  }
  .title {
    font-size: 32px;
    font-weight: bold;
    color: #ffdd00;
  }
  .size {
    width: 750px;
    max-height: 52px;
    color: #ffffff;
    white-space: pre-wrap; /* 保留空白符序列，但是当到达容器边界时进行换行 */
    overflow-wrap: break-word; /* 在长单词或URL地址内部进行换行 */
    word-break: break-all; /* 允许在任意字符间断开（用于CJK文字等） */
  }
}
.SampleBoxRigth {
  height: 100%;
}
</style>

